package com.blue.firecat.utils.email;

import com.alibaba.fastjson.JSON;
import com.blue.firecat.utils.property.PropertiesUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.mail.Session;
import javax.mail.internet.MimeUtility;

public class EmailUtils {
    private static Logger logger = LogManager.getLogger(EmailUtils.class.getName());
    private String name;//收件人姓名
    private String to;//收件人账号
    private String code;//验证码(对外收入的参数)

    private String smtp; //服务器主机
    private String username; //用户名
    private String password;//密码
    private String from;//发件人
    private String subject; //主题
    private String content;//内容

    public EmailUtils(String name, String to, String code) {
        logger.info("send email ..name:{},mto:{},code:{}", name, to, code);
        this.name = name;
        this.to = to;
        this.code = code;
        try {
//            subject = "火狸游戏订单管理系统的邮件提醒!";//获取主题
            subject = MimeUtility.encodeText("火狸游戏订单管理系统的邮件提醒!", "UTF-8", "B");

            smtp = PropertiesUtil.getProperty("smtp");//获取服务器主机
            username = PropertiesUtil.getProperty("username");//获取用户名
            password = PropertiesUtil.getProperty("password");//获取密码
            from = PropertiesUtil.getProperty("from");//获取发件人
        } catch (Exception e) {
//            smtp = "smtp.163.com";
//            username = "chengziwangluo123@163.com";
//            password = "chengzi123";
//            from = "chengziwangluo123@163.com";

            smtp = "smtp.163.com";
            username = "18770913803@163.com";
            password = "z123456";
            from = "18770913803@163.com";
        }
    }

    public void send(int type) {
        if (type == 0) {//发送验证码
            content = "验证码:<font color='red',size=8>" + this.code + "</font>,如果获取失败,请联系管理员!";
        } else if (type == 1) {//金额
            content = "将获得获得资金:<font color='red',size=8>" + this.code + " 元</font>,如果获取失败,请联系管理员!";
        } else if (type == 2) {//登陆不上
            content = "订单异常,编号为:<font color='red',size=8>" + this.code + " </font>,请及时处理!";
        } else if (type == 3) {//审核通过
            content = "订单审核通过,订单号为:<font color='red',size=8>" + this.code + " </font>,请尽快代练吧";
        } else if (type == 4) {//指定打手
            content = "订单指派成功,订单号为:<font color='red',size=8>" + this.code + " </font>,请尽快确认吧";
        } else if (type == 5) {//指定打手
            content = "登陆密码为:<font color='red',size=8>" + this.code + " </font>,请尽快登陆修改吧";
        } else if (type == 6) {//指定打手
            content = "订单剩余验收时间不足一天了,订单号为:<font color='red',size=8>" + this.code + " </font>,请尽快完成吧";
        } else if (type == 7) {//指定打手
            content = "订单剩余验收时间不足三小时了,订单号为:<font color='red',size=8>" + this.code + " </font>,请尽快完成吧";
        } else if (type == 8) {//指定打手
            content = "新用户注册,注册信息为:<font color='red',size=5>" + this.code + " </font>,请登陆系统查看并确认是否审核吧";
        } else if (type == 9) {//指定打手
            content = "恭喜成为本系统:" + this.code + ",请尽快登陆使用并且补全个人信息吧!";
        } else if (type == 10) {//指定打手
            content = "恭喜,审核成功,请尽快登陆使用并且补全个人信息吧,否则可能影响管理员给您打款!";
        } else {
            content = null;
        }
        this.send();
    }

    private void send() {
        Session session = Email.createSession(smtp, username, password);//得到session
        MailVo mail = new MailVo(from, to, subject, content);//创建邮件对象
        try {
            Email.send(session, mail);
            logger.info("mail send success:{}", JSON.toJSONString(mail));
        } catch (Exception e) {
            tryAgain();
            logger.error("mail send fail:{}\n{}", JSON.toJSONString(mail), e);
            e.printStackTrace();
        }
    }

    private void tryAgain() {
        Session session = Email.createSession(smtp, "18770913803@163.com", "z123456");//得到session
        MailVo mail = new MailVo("18770913803@163.com", to, subject, content);//创建邮件对象
        try {
            Email.send(session, mail);
            logger.info("mail tryAgain success:{}", JSON.toJSONString(mail));
        } catch (Exception e) {
            logger.error("mail tryAgain fail:{}\n{}", JSON.toJSONString(mail), e);
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1; i++) {
            EmailUtils email = new EmailUtils("", "438443117@qq.com", "123456789");
            email.send(i);
        }

    }

}
//java处理url中的特殊字符
//1.+ 表示空格（在 URL 中不能使用空格）          %20 
//2./ 分隔目录和子目录                                              %2F 
//3.? 分隔实际的 URL 和参数                                   %3F 
//4.% 指定特殊字符                                                    %25 
//5.# 表示书签                                                             %23 
//6.& URL 中指定的参数间的分隔符             	%26 